package version_2;

import java.io.File;
import java.io.IOException;

/**
 * @author 王子铮 11923040205
 */
public class Test2 {
    public static void main(String []args) throws IOException {

        //存放姓名的数组
        String[] name_list={"甲","乙","丙","丁","戊"};

        //存放对象的数组
        Person[] list =new Person[5];

        //随机生成5个对象，并存放在数组中
        for (int i=0;i<5;i++){
            int random= (int) (Math.random() * 6);
            switch (random){
                case 0:list[i]=new Person(name_list[i]);break;
                case 1:list[i]=new Student(name_list[i]);break;
                case 2:list[i]=new Employee(name_list[i]);break;
                case 3:list[i]=new Faculty(name_list[i]);break;
                case 4:list[i]=new staff(name_list[i]);break;
                case 5:list[i]=new Postgraduate(name_list[i]);break;
            }
        }

        //打印信息
        printMessages(list);

        //调用函数进行序列化
        serialize(list);
        System.out.println("已完成序列化");
    }

    /**打印对象信息*/
    public static void printMessages(Person[] persons){
        for (Person person : persons) {
            System.out.println(person.toString());
        }
    }

    /**
     * 将每一个对象序列化在object文件夹中的文件中。
     * 以编号命名
     */
    public static void serialize(Person[] persons) throws IOException {
        for (int i=0;i<persons.length;i++){
            String file_name="object//"+i+".text";
            //ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File(file_name)));
            //oos.writeObject(persons[i]);
            persons[i].writeToFile(new File(file_name));
        }
    }
}

